import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TcpEchoClient1 {
    Socket socket = null;

    //传入服务器的IP地址和端口号
    public TcpEchoClient1(String serverIp,int serverPort) throws IOException {
        socket = new Socket(serverIp,serverPort);
    }

    public void start(){
        Scanner scanner = new Scanner(System.in);

        try(InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream()){
            Scanner scannerNet = new Scanner(inputStream);
            PrintWriter printWriter = new PrintWriter(outputStream);
            while (true){
                //1.从控制台读取请求
                String request = scanner.next();
                //2.发送请求到服务器
                printWriter.println(request);
                printWriter.flush();
                //3.读取服务器的响应
                String response = scannerNet.next();
                //4.打印响应
                System.out.println(response);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws IOException {
        TcpEchoClient1 tcpEchoClient1 = new TcpEchoClient1("127.0.0.1",9090);
        tcpEchoClient1.start();
    }

}
